<?php
/**
 * Created by 老吴.
 * UserMsg:砥砺前行，扬帆起航
 * email:cwwx0128@qq.com
 * QQ:1113249273
 * QQ群:925283872
 * 微信:cww0128
 * Date: 2021/5/7
 * Time: 11:03
 */

namespace app\admin\controller;


use think\Loader;

class Authbase
{
    /**
     * 用户登录-包含静默授权
     */
    public function login()
    {
        Loader::import('ali.AopClientSelf', EXTEND_PATH);
        Loader::import('ali.request.AlipaySystemOauthTokenRequestSelf', EXTEND_PATH);
//        $this->success('', $param);
        $aop = new \AopClientSelf();
        $param = $this->request->param();

        $aop->gatewayUrl = $this->gatewayUrl;
        $aop->appId = $this->appId;
        $aop->rsaPrivateKey = $this->rsaPrivateKey;
        $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset = 'utf-8';
        $aop->format = 'json';
        $request = new \AlipaySystemOauthTokenRequestSelf();
        $request->setGrantType("authorization_code");
        $request->setCode($param['code']);
        $result = $aop->execute($request);
        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        if ($param['type'] == 'auth_base') {
            if (!$result->alipay_system_oauth_token_response->user_id) {
                $this->error('授权失败');
            } else {
                $openid = $result->alipay_system_oauth_token_response->user_id;
                $userinfo = \app\admin\model\User::where(['openid' => $openid])->find();
                if ($userinfo) {
                    $userinfo->save();
                    $this->auth->direct($userinfo['id']);
                } else {
                    $user = new \app\admin\model\User();
                    $user->data([
                        'nickname' => $openid . 'auth_base',
                        'status' => 'normal',
                        'openid' => $openid
                    ]);
                    $user->save();
                    $this->auth->direct($user->id);
                }
                $this->success('登录成功', $this->auth->getUserinfo());

            }
        } else {
            $accessToken = $result->$responseNode->access_token;
            $ret = self::getUserInfoAli($accessToken);

            if ($ret->alipay_user_info_share_response->code == '10000') {
                $userinfo = \app\admin\model\User::where(['openid' => $ret->alipay_user_info_share_response->user_id])->find();
                if ($userinfo) {
                    $userinfo->nickname = $ret->alipay_user_info_share_response->nick_name;
                    $userinfo->avatar = $ret->alipay_user_info_share_response->avatar;
                    $userinfo->gender = $ret->alipay_user_info_share_response->gender == 'm' ? '1' : '0';
                    $userinfo->city = $ret->alipay_user_info_share_response->city;
                    $userinfo->province = $ret->alipay_user_info_share_response->province;
                    $userinfo->country = '中国';
                    $userinfo->save();
                    $this->auth->direct($userinfo['id']);
                } else {
                    $user = new \app\admin\model\User();
                    $user->data([
                        'nickname' => $ret->alipay_user_info_share_response->nick_name,
                        'avatar' => $ret->alipay_user_info_share_response->avatar,
                        'gender' => $ret->alipay_user_info_share_response->gender == 'm' ? '1' : '0',
                        'city' => $ret->alipay_user_info_share_response->city,
                        'province' => $ret->alipay_user_info_share_response->province,
                        'country' => '中国',
                        'status' => 'normal',
                        'openid' => $ret->alipay_user_info_share_response->user_id
                    ]);
                    $user->save();
                    $this->auth->direct($user->id);
                }
                $this->success('登录成功', $this->auth->getUserinfo());
            } else {
                $this->error($ret->alipay_user_info_share_response->msg . '错误码：' . $ret->alipay_user_info_share_response->code);
            }
        }


//        $this->success('', $ret);


    }

}